package com.chen.demo.javase.algorithm.leetcode.editor.cn;
//给你链表的头结点 head ，请将其按 升序 排列并返回 排序后的链表 。 
//
// 进阶： 
//
// 
// 你可以在 O(n log n) 时间复杂度和常数级空间复杂度下，对链表进行排序吗？ 
// 
//
// 
//
// 示例 1： 
//
// 
//输入：head = [4,2,1,3]
//输出：[1,2,3,4]
// 
//
// 示例 2： 
//
// 
//输入：head = [-1,5,3,4,0]
//输出：[-1,0,3,4,5]
// 
//
// 示例 3： 
//
// 
//输入：head = []
//输出：[]
// 
//
// 
//
// 提示： 
//
// 
// 链表中节点的数目在范围 [0, 5 * 104] 内 
// -105 <= Node.val <= 105 
// 
// Related Topics 排序 链表 
// 👍 954 👎 0

import java.util.Scanner;

public class SortList {
    public static void main(String[] args) {
        Solution solution = new SortList().new Solution();

        ListNode listNode = solution.sortList(initList(""));
        printList(listNode);

    }

//leetcode submit region begin(Prohibit modification and deletion)

    /**
     * Definition for singly-linked list.
     * public class ListNode {
     * int val;
     * ListNode next;
     * ListNode() {}
     * ListNode(int val) { this.val = val; }
     * ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     * }
     */
    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }


    class Solution {
        /**
         * 归并排序
         *
         * @param head head
         * @return head
         */
        public ListNode sortList(ListNode head) {
            if (head == null || head.next == null) {
                return head;
            }
            // 通过快慢指针找出链表尾部，然后端口链表
            ListNode slow = head;
            ListNode fast = head.next;
            while (fast != null && fast.next != null) {
                slow = slow.next;
                fast = fast.next.next;
            }
            ListNode temp = slow.next;
            //断开链表
            slow.next = null;
            ListNode left = sortList(head);
            ListNode right = sortList(temp);
            return mergeTwoLists(left, right);
        }
    }

    public static void printList(ListNode head) {
        ListNode temp = head;
        while (temp != null) {
            System.out.print("" + temp.val + " ");
            temp = temp.next;
        }
        System.out.println("");
    }

    public static ListNode initList(String listNo) {
        System.out.println("链表长度:");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        int length = Integer.parseInt(s);
        ListNode head = null;
        ListNode temp = null;
        System.out.println("以此输入链表数据:");
        while (length > 0) {
            if (head == null) {
                temp = new ListNode(Integer.parseInt(scanner.next()));
                head = temp;
            } else {
                temp.next = new ListNode(Integer.parseInt(scanner.next()));
                temp = temp.next;
            }

            length--;
        }
        return head;
    }

    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        }
        if (l2 == null) {
            return l1;
        }
        ListNode head = null;
        ListNode tmp;
        ListNode tail = null;
        while (l1 != null && l2 != null) {
            // l1 向后移动
            if (l1.val <= l2.val) {
                tmp = l1;
                l1 = l1.next;
            } else {
                // l2 向后移动
                tmp = l2;
                l2 = l2.next;
            }
            if (head == null) {
                head = tmp;
                tail = tmp;
            } else {
                tail.next = tmp;
                tail = tail.next;
            }
        }
        if (l1 != null) {
            tail.next = l1;
        }
        if (l2 != null) {
            tail.next = l2;
        }
        return head;
    }
//leetcode submit region end(Prohibit modification and deletion)

}